//
//  HEMyOrderFooterCell.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/11/5.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEMyOrderFooterCell.h"

@interface HEMyOrderFooterCell ()

@property (nonatomic, strong) UILabel *totalLb;
@property (nonatomic, strong) HEOrderBottomView *bottomView;

@end

@implementation HEMyOrderFooterCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
     if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
         self.backgroundColor = [UIColor clearColor];
         self.selectionStyle = UITableViewCellSelectionStyleNone;
         [self initUI];
         
         
         UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectClick)];
         [self.contentView addGestureRecognizer:tap];
     }
     return self;;
 }

- (void)selectClick {
    if ([_delegate respondsToSelector:@selector(footerSelect:)]) {
        [_delegate footerSelect: _model];
    }
}

- (void)initUI {
    
    UIView *warp = [UIView new];
    warp.backgroundColor = KWhiteColor;
    [self.contentView addSubview:warp];

    [warp mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).mas_offset(0);
        make.left.equalTo(self.contentView).mas_offset(10);
        make.right.equalTo(self.contentView).mas_offset(-10);
//        make.height.mas_greaterThanOrEqualTo(15);
        make.bottom.equalTo(self.contentView).mas_offset(0);
    }];
    [warp hg_setCornerOnBottomWithRadius:10.0f];
    
    
    // 总计
    _totalLb = [UILabel new];
    _totalLb.text = @"-";
    _totalLb.font = DYFont(15);
    _totalLb.textAlignment = NSTextAlignmentRight;
    [warp addSubview:_totalLb];
    
    UIView *footerLine = [UIView new];
    footerLine.backgroundColor = CSplitLineColor;
    [warp addSubview:footerLine];
        
    [_totalLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(warp).mas_offset(1);
        make.right.equalTo(warp).mas_offset(-5);
        make.left.equalTo(warp).mas_offset(15);
    }];
    
    [footerLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_totalLb.mas_bottom).mas_offset(10);
        make.height.mas_offset(1);
        make.left.equalTo(warp).mas_offset(15);
        make.right.equalTo(warp).mas_offset(-10);
    }];
    
    
    _bottomView = [[HEOrderBottomView alloc] init];
    [self.contentView addSubview:_bottomView];
    [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(footerLine.mas_bottom).mas_offset(10);
        make.left.equalTo(warp).mas_offset(10);
        make.right.equalTo(warp).mas_offset(-10);
        make.height.mas_equalTo(30);
        make.bottom.mas_equalTo(self.contentView).offset(-10);
    }];
    
    __weak typeof(self) weakSelf = self;
    _bottomView.oprationBlock = ^(HEOrderBottomBtnType type, HEOrderModel * _Nonnull model) {
        if ([weakSelf.delegate respondsToSelector:@selector(bottomOperationWithModel:type:)]) {
            [weakSelf.delegate bottomOperationWithModel:model type:type];
        }
    };
}

- (void)setModel:(HEOrderModel *)model {
    _model = model;
    _totalLb.text = model.KtotalAmount;
    _bottomView.model = model;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}

@end




@interface HEOrderBottomView ()

@property (nonatomic, strong) NSMutableArray *ButtonArray;
@property (nonatomic, strong) NSMutableArray *itemsArr;
@property (nonatomic, strong) NSArray *btnModels;

@end

static CGFloat margin = 10.f;
#define ButtonW 80

@implementation HEOrderBottomView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setUpUI];
    }
    return self;
}

-(void)setUpUI {
   
    for (int i = 0; i < 3; i++) {
        
        NSInteger row1 = i/3;
        NSInteger colum = i%3;
        
        QMUIGhostButton *checkBtn = [[QMUIGhostButton alloc] initWithGhostColor:CNavBgColor];
        checkBtn.titleLabel.font = DYBlodFont(14);
        [checkBtn setTitle:@"查看订单" forState:UIControlStateNormal];
        checkBtn.frame = CGRectMake((margin + ButtonW)*colum, (margin + ButtonW)*row1, ButtonW, 28);
        checkBtn.hidden = YES;
        checkBtn.tag = i;
        [self addSubview:checkBtn];
        
        [checkBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self);
            if (i == 0) {
                make.right.equalTo(self);
            }else {
                QMUIGhostButton *latBtn = self.ButtonArray[i-1];
                make.right.equalTo(latBtn.mas_left).mas_offset(-margin);
            }
            make.height.mas_equalTo(28);
            make.width.mas_equalTo(ButtonW);
        }];
        [self.ButtonArray addObject:checkBtn];
    }
}

- (void)setModel:(HEOrderModel *)model {
    
    _model = model;
    
    _btnModels = @[];
    
    // 居家照料  护工订单
    if (model.channel == HEChannelTypeHomeCare && model.status != HEOrderStatusWaitPay) {
        switch (model.welfareStatus) {
            case HEOrderWelfareStatusEvaluated: // 待评价
            {
                _btnModels = @[@{@"name":@"查看订单", @"color":CNavBgColor, @"type": [NSNumber numberWithInteger:HEOrderBottomBtnTypeCheckDetails]},
                         @{@"name":@"评价", @"color":CPriceColor, @"type": [NSNumber numberWithInteger:HEOrderBottomBtnTypeComment]}];
            }
                break;
            case HEOrderWelfareStatusCanceled:
            case HEOrderWelfareStatusAutoRefurn:
            {
                _btnModels = @[@{@"name":@"查看订单", @"color":CNavBgColor, @"type": [NSNumber numberWithInteger:HEOrderBottomBtnTypeCheckDetails]},
                         @{@"name":@"删除订单", @"color":CNavLightBlackColor, @"type": [NSNumber numberWithInteger:HEOrderBottomBtnTypeDelete]}];
            }
                break;
            default:
                _btnModels = @[@{@"name":@"查看订单", @"color":CNavBgColor, @"type": [NSNumber numberWithInteger:HEOrderBottomBtnTypeCheckDetails]}];
                break;
        }
     
    }else {
        switch (model.status) {
            case HEOrderStatusWaitPay:
            {
                _btnModels = @[@{@"name":@"查看订单", @"color":CNavBgColor, @"type": [NSNumber numberWithInteger:HEOrderBottomBtnTypeCheckDetails]},
                         @{@"name":@"删除订单", @"color":CNavLightBlackColor, @"type": [NSNumber numberWithInteger:HEOrderBottomBtnTypeDelete]},
                         @{@"name":@"取消订单", @"color":CNavLightBlackColor, @"type": [NSNumber numberWithInteger:HEOrderBottomBtnTypeCancelOrder]},];
            }
                break;
            case HEOrderStatusWaitDeliver:
            {
                _btnModels = @[@{@"name":@"查看订单", @"color":CNavBgColor, @"type": [NSNumber numberWithInteger:HEOrderBottomBtnTypeCheckDetails]}];
            }
                break;
            case HEOrderStatusDelivered:
            {
                _btnModels = @[@{@"name":@"查看订单", @"color":CNavBgColor, @"type": [NSNumber numberWithInteger:HEOrderBottomBtnTypeCheckDetails]},
                               @{@"name":@"确认收货", @"color":CNavBgColor, @"type": [NSNumber numberWithInteger:HEOrderBottomBtnTypeConfirmGoods]},
                         @{@"name":@"查看物流", @"color":CNavLightBlackColor, @"type": [NSNumber numberWithInteger:HEOrderBottomBtnTypeChecklogistics]}];
            }
                break;
            case HEOrderStatusWaitComment:
            {
                _btnModels = @[@{@"name":@"查看订单", @"color":CNavBgColor, @"type": [NSNumber numberWithInteger:HEOrderBottomBtnTypeCheckDetails]},
                         @{@"name":@"评价", @"color":CPriceColor, @"type": [NSNumber numberWithInteger:HEOrderBottomBtnTypeComment]}];
            }
                break;
            case HEOrderStatusFinish:
            case HEOrderStatusClosed:
            {
                _btnModels = @[@{@"name":@"查看订单", @"color":CNavBgColor, @"type": [NSNumber numberWithInteger:HEOrderBottomBtnTypeCheckDetails]},
                         @{@"name":@"删除订单", @"color":CNavLightBlackColor, @"type": [NSNumber numberWithInteger:HEOrderBottomBtnTypeDelete]}];
            }
                break;
            default:
                _btnModels = @[@{@"name":@"查看订单", @"color":CNavBgColor, @"type": [NSNumber numberWithInteger:HEOrderBottomBtnTypeCheckDetails]}];
                break;
        }
    }
    
    [self.ButtonArray enumerateObjectsUsingBlock:^(UIButton *  _Nonnull button, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx < self.btnModels.count) {
            button.hidden = NO;
            NSDictionary *dict = self.btnModels[idx];
            QMUIGhostButton *latBtn = self.ButtonArray[idx];
            [latBtn setTitle:dict[@"name"] forState:UIControlStateNormal];
            [latBtn setTitleColor:dict[@"color"] forState:UIControlStateNormal];
            latBtn.ghostColor = dict[@"color"];
            [latBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        }else{
            button.hidden = YES;
        }
    }];
}

- (void)btnClick:(QMUIGhostButton *)btn {
    NSInteger tag = btn.tag;
    NSDictionary *dict = self.btnModels[tag];
    NSNumber *type = dict[@"type"];
    if (self.oprationBlock) {
        self.oprationBlock(type.integerValue, _model);
    }
}

#pragma mark - Getter

- (NSMutableArray *)ButtonArray {
    if(!_ButtonArray){
        _ButtonArray = [[NSMutableArray alloc] init];
    }
    return _ButtonArray;
}


@end

